package top.zywork.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.StringUtils;
import top.zywork.constant.CommonConstant;
import top.zywork.dos.SchoolDO;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.SchoolDTO;
import top.zywork.query.SchoolQuery;
import top.zywork.service.SchoolService;
import top.zywork.vo.PagerVO;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.SchoolVO;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/school")
@Slf4j
public class SchoolController {

    @Autowired
    private SchoolService schoolService;

    /**
     * 学校选择列表 这里目前先是简单的查询没有根据省市区来分类。
     * @return
     */
    @PostMapping("any/getList")
    public ResponseStatusVO getList(){
        List<SchoolDO> is_active = schoolService.list(new QueryWrapper<SchoolDO>().eq("is_active", CommonConstant.IS_ACTIVE_TRUE));
        return ResponseStatusVO.ok("查询成功",is_active);
    }

    @PostMapping("admin/save")
    @SysLog(description = "后台保存SchoolVO")
    public ResponseStatusVO save(@RequestBody @Validated SchoolVO schoolVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        schoolService.save(BeanUtils.copy(schoolVO, SchoolDO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存SchoolVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<SchoolVO> schoolVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        BeanUtils.copyListObj(schoolVOList, SchoolDO.class);

        List<SchoolDO> list;
        list = schoolVOList.stream().map(e->BeanUtils.copy(e,SchoolDO.class)).collect(Collectors.toList());

        schoolService.saveBatch(list);
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除SchoolVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        schoolService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除SchoolVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        List<String> id = new ArrayList<>();
        Collections.addAll(id, ids);
        schoolService.removeByIds(id);
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新SchoolVO")
    public ResponseStatusVO update(@RequestBody @Validated SchoolVO schoolVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        boolean result = schoolService.updateById(BeanUtils.copy(schoolVO, SchoolDO.class));
        if (result) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新SchoolVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<SchoolVO> schoolVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        List<SchoolDO> list;
        list = schoolVOList.stream().map(e->BeanUtils.copy(e,SchoolDO.class)).collect(Collectors.toList());
        schoolService.updateBatchById(list);
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结SchoolVO")
    public ResponseStatusVO active(@RequestBody SchoolVO schoolVO) {
        schoolService.update(new UpdateWrapper<SchoolDO>().eq("id",schoolVO.getId())
        .set("is_active",schoolVO.getIsActive()));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结SchoolVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<SchoolVO> schoolVOList) {
        List<SchoolDO> list;
        list = schoolVOList.stream().map(e->BeanUtils.copy(e,SchoolDO.class)).collect(Collectors.toList());
        schoolService.updateBatchById(list);
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        SchoolVO schoolVO = new SchoolVO();
        Object obj = schoolService.getById(id);
        if (obj != null) {
            schoolVO = BeanUtils.copy(obj, SchoolVO.class);
        }
        return ResponseStatusVO.ok("查询成功", schoolVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        IPage page = schoolService.page(null);
        PagerDTO pagerDTO = PagerDTO.builder().total(page.getTotal()).rows(page.getRecords()).build();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), SchoolVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody SchoolQuery schoolQuery) {
        PagerDTO pagerDTO = schoolService.listAllByCondition(schoolQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), SchoolVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody SchoolQuery schoolQuery) {
        PagerDTO pagerDTO = schoolService.listPageByCondition(schoolQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), SchoolVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }


}
